package com.zretc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.commons.AjaxResponse;
import com.zretc.dao.FdtypeDao;
import com.zretc.dao.impl.FdtypeDaoImpl;
import com.zretc.entity.Fdtype;
import com.zretc.service.FdtypeService;
import com.zretc.service.impl.FdtypeServiceImpl;
import com.zretc.tools.PageData;

/**
 * 处理新闻类型的servlet 以后我们的servlet就直接调用service层 不再直接调用dao层了
 */
@WebServlet("/FdTypeServlet")
public class FdTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	FdtypeDao fdtypeDao=new FdtypeDaoImpl();
	FdtypeService fdtypeService=new FdtypeServiceImpl();
	// 接口引用 指向 实现类对象
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		// 标记当前为何种操作的变量
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if ("".equals(op)) {
			doQuery(request, response);

		} else if ("add".equals(op)) {
			// 添加类型的操作
			doAdd(request, response);
		}else if ("queryByPage".equals(op)) {
			// 添加类型的操作
			doqueryByPage(request, response);
		}else if ("ajaxReq".equals(op)) {
			// 分页有关的查询 数据处理 页面跳转等
			doAjax(request, response);

		}else if ("ajaxtypename".equals(op)) {
			// 分页有关的查询 数据处理 页面跳转等
			doAjaxGetName(request,response);

		}

	}
	
	
	/**
	 * 处理ajax请求，判断是否存在对应的typename
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doAjaxGetName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");		
		String fdtypename = request.getParameter("fdtypename");
		System.out.println("fdtypename :"+fdtypename);
		// 2 调用 service的方法,查询是否存在typename对象
		Fdtype fdtype = fdtypeService.getFdtypeName(fdtypename);
		//我们以后再进行ajax请求的时候，我们希望返回结果是一个标准的格式
		// AjaxResponse: ->msg status data 等等
		//目前可以简单处理，直接返回一个字符串		
		String msg = "exists";
		if (fdtype==null) {
			//?没有找到
			msg="notexists";
		}
//		ajaxResponse ajaxResponse = new  AjaxResponse(msg,200);
		AjaxResponse ajaxResponse=new AjaxResponse(msg, 200);
		// 3将list转换为json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(ajaxResponse);

		// 4 响应
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(jsonString);
		// 5.关闭
		outPrintWriter.close();
	}
	
	
	private void doqueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 2获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		//页码默认为1
		int page=1;
		//每页记录数默认为10
		int pageSize=10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 3 调用 dao的方法
		PageData<Fdtype> fd = fdtypeService.getFdtypeByPage(page, pageSize, keywords);

		// 4传递list给jsp
		request.setAttribute("fd", fd);
		request.setAttribute("keywords", keywords);
		// 5 转发
		request.getRequestDispatcher("fdtypepage.jsp").forward(request, response);
	}

	/**
	 * 添加功能的操作
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1获取请求的参数,要先判断是否为空[需要有服务器端验证]
		String fdtypename = request.getParameter("fdtypename");
		

		// 2构造一个fdtype对象
		Fdtype fdtype = new Fdtype(fdtypename);

		boolean flag = fdtypeService.addFdtype(fdtype);
		// 4根据返回结果进行转发或者提示等
		if (flag) {
			// 转发到显示servelt上进行查询，直接调用doQuery的方法即可。
			doQuery(request, response);
		} else {
			// 转发到回到newstype增加的页面
			// 失败分两种情况，一种是如果名字重复；另外可能发生异常
			request.setAttribute("msg", "增加失败！请重新添加。");
			request.getRequestDispatcher("addfdtype.jsp").forward(request, response);

		}
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 2获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}

		// 3 调用 dao的方法
		List<Fdtype> list = fdtypeDao.getFdtypes(keywords);

		// 4传递list给jsp
		request.setAttribute("list", list);
	
		// 5 转发
		request.getRequestDispatcher("fdtypepage.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void doAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.设置响应头的contentType
//		response.setContentType("application/json;charset=utf-8");
		response.setContentType("application/json;charset=utf-8");
		// 2 调用 service的方法
		List<Fdtype> list = fdtypeService.getTypes();

		// 3将list转换为json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(list);

		// 4 响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);

		// 5.关闭
		pw.close();
	}

}
